#include "dialogs.h"

syntaxError::syntaxError(QWidget *parent, const QString &reason, int ln): 
	QDialog(parent), line(ln){

	setMinimumWidth(480);
	vLayout = new QVBoxLayout;
	label = new QLabel;
	high = new highlighter;
	setLayout(vLayout);
	createWarning();
	vLayout->addWidget(label);
	label->setText(reason);
	createOkButton();

}

void syntaxError::createWarning(){

	QHBoxLayout *layout = new QHBoxLayout;
	QLabel *warning = new QLabel();
	QLabel *message = new QLabel(tr("<b>Obsah souboru není validní.</b>"));
	QLabel *gap = new QLabel;
	QPixmap map(":img/warning.png"); 
	
	warning->setPixmap(map);
	layout->addWidget(warning, 0, Qt::AlignLeft);
	layout->addWidget(message, 0, Qt::AlignLeft);
	layout->addWidget(gap, 1, Qt::AlignLeft);
	vLayout->addLayout(layout);

}

void syntaxError::createOkButton(){

	QHBoxLayout *layout = new QHBoxLayout;
	QPushButton *okButton = new QPushButton(tr("Ok"));
	QPushButton *editButton = new QPushButton(tr("Upravit"));
	QLabel *gap = new QLabel;
	
	layout->addWidget(gap, 1);
	layout->addWidget(okButton, 0, Qt::AlignRight);
	layout->addWidget(editButton, 0, Qt::AlignRight);
	vLayout->addLayout(layout);
	connect(okButton, SIGNAL(clicked()), this, SLOT(okClicked()));
	connect(editButton, SIGNAL(clicked()), this, SLOT(editClicked()));

}

void syntaxError::okClicked(){

	state = OK;
	accept();

}

void syntaxError::editClicked(){

	state = EDIT;
	accept();

}

void syntaxError::setFileView(const QString &fileName){

	QFile file(fileName);
	
	if(file.open(QFile::ReadOnly | QFile::Text)){
		codeText *text = new codeText;
		QGroupBox *box = new QGroupBox;
		QVBoxLayout *lay = new QVBoxLayout;
		
		lay->setContentsMargins(0, 0, 0, 0);
		text->setReadOnly(true);
		text->setPlainText(file.readAll());
		high->setDocument(text->document());
		text->highlighSpecificLine(line);
		vLayout->addWidget(box);
		box->setLayout(lay);
		box->setTitle(fileName);
		lay->addWidget(text);
		
	}

}

syntaxError::states syntaxError::getState() const{

	return state;

}

algorithmProblem::algorithmProblem(QWidget *parent, const QString problem): QDialog(parent){

	vLayout = new QVBoxLayout;
	reason = new QLabel(problem);
	setLayout(vLayout);
	createWarning();
	createOkButton();

}

void algorithmProblem::createWarning(){

	QHBoxLayout *layout = new QHBoxLayout;
	QLabel *warning = new QLabel();
	QLabel *message = new QLabel(tr("<b>Chyba při zpracování nástroje.</b>"));
	QLabel *gap = new QLabel;
	QPixmap map(":img/warning.png"); 
	
	warning->setPixmap(map);
	layout->addWidget(warning, 0, Qt::AlignLeft);
	layout->addWidget(message, 0, Qt::AlignLeft);
	layout->addWidget(gap, 1, Qt::AlignLeft);
	vLayout->addLayout(layout);
	vLayout->addWidget(reason);

}

void algorithmProblem::createOkButton(){

	QHBoxLayout *layout = new QHBoxLayout;
	QPushButton *button = new QPushButton(tr("Ok"));
	
	layout->addWidget(button, 0, Qt::AlignRight);
	vLayout->addLayout(layout);
	connect(button, SIGNAL(clicked()), this, SLOT(close()));

}

saveOrNot::saveOrNot(QWidget *widget, const QString &fileName) : QDialog(widget){

	vLayout = new QVBoxLayout;
	setLayout(vLayout);
	createText(fileName);
	createButtons();

}

void saveOrNot::createText(const QString &fileName){

	QHBoxLayout *layout = new QHBoxLayout;
	QLabel *warning = new QLabel();
	QLabel *message = new QLabel(tr("Soubor <b>\"%1\"</b><br /> byl změněn. Přejete si ho uložit ?").arg(fileName));
	QLabel *gap = new QLabel;
	QPixmap map(":img/question.png"); 
	
	warning->setPixmap(map);
	layout->addWidget(warning);
	layout->addWidget(message, 0, Qt::AlignLeft);
	layout->addWidget(gap, 1, Qt::AlignLeft);
	vLayout->addLayout(layout);

}

void saveOrNot::createButtons(){

	QHBoxLayout *layout = new QHBoxLayout;
	QPushButton *yes = new QPushButton(tr("Uložit"));
	QPushButton *no = new QPushButton(tr("Neukládat"));
	QPushButton *cancel = new QPushButton(tr("Zrušit"));
	QLabel *gap = new QLabel;

	connect(yes, SIGNAL(clicked()), this, SLOT(okClicked()));
	connect(no, SIGNAL(clicked()), this, SLOT(noClicked()));
	connect(cancel, SIGNAL(clicked()), this, SLOT(cancelClicked()));
	layout->addWidget(gap, 1);
	layout->addWidget(yes, 0, Qt::AlignRight);
	layout->addWidget(no, 0, Qt::AlignRight);
	layout->addWidget(cancel, 0, Qt::AlignRight);
	vLayout->addLayout(layout);

}

saveOrNot::saveState saveOrNot::getState() const{

	return state;

}

void saveOrNot::okClicked(){

	state = YES;
	accept();

}

void saveOrNot::noClicked(){

	state = NO;
	accept();

}

void saveOrNot::cancelClicked(){

	state = CANCEL;
	accept();
	
}

fileModifiedDialog::fileModifiedDialog(QWidget *parent, const QString &fileName): QDialog(parent){

	vLayout = new QVBoxLayout;
	
	setLayout(vLayout);
	createText(fileName);
	createButtons();

}

void fileModifiedDialog::createText(const QString &fileName){

	QHBoxLayout *layout = new QHBoxLayout;
	QLabel *warning = new QLabel();
	QLabel *message = new QLabel(tr("Soubor <b>\"%1\"</b><br /> byl změněn druhou stranou. Přejete si ho ?").arg(fileName));
	QLabel *gap = new QLabel;
	QPixmap map(":img/question.png"); 
	
	warning->setPixmap(map);
	layout->addWidget(warning);
	layout->addWidget(message, 0, Qt::AlignLeft);
	layout->addWidget(gap, 1, Qt::AlignLeft);
	vLayout->addLayout(layout);

}

void fileModifiedDialog::createButtons(){

	QHBoxLayout *layout = new QHBoxLayout;
	QPushButton *save = new QPushButton(tr("Uložit"));
	QPushButton *reload = new QPushButton(tr("Znovu načíst"));
	QPushButton *close = new QPushButton(tr("Zavřít"));
	QLabel *gap = new QLabel;

	connect(save, SIGNAL(clicked()), this, SLOT(saveClicked()));
	connect(reload, SIGNAL(clicked()), this, SLOT(reloadClicked()));
	connect(close, SIGNAL(clicked()), this, SLOT(closeClicked()));
	layout->addWidget(gap, 1);
	layout->addWidget(save, 0, Qt::AlignRight);
	layout->addWidget(reload, 0, Qt::AlignRight);
	layout->addWidget(close, 0, Qt::AlignRight);
	vLayout->addLayout(layout);

}

void fileModifiedDialog::saveClicked(){

	state = SAVE;
	accept();

}

void fileModifiedDialog::reloadClicked(){

	state = RELOAD;
	accept();

}

void fileModifiedDialog::closeClicked(){

	state = CLOSE;
	accept();
	
}

fileModifiedDialog::modifiedState fileModifiedDialog::getState() const{

	return state;

}

textEditDialog::textEditDialog(QWidget *parent): QDialog(parent){

	vLayout = new QVBoxLayout(this);
	edit = new codeEdit(this);
	
	vLayout->setContentsMargins(1, 0, 1, 1);
	vLayout->addWidget(edit);
	connect(edit, SIGNAL(textChanged()), this, SLOT(changed()));

}

void textEditDialog::changed(){

	//setWindowModified(true);

}

void textEditDialog::setFile(const QString &fileName){

	setWindowTitle(QFileInfo(fileName).baseName());
	edit->setFile(fileName);

}

bool textEditDialog::isModified() const{

	return edit->isModified();

}

QString textEditDialog::getFile() const{

	return edit->getFile();

}

bool textEditDialog::saveAs(){

	QString fileName = QFileDialog::getSaveFileName(this, tr("Ulozit jako"), getFile());
	
	if(fileName.isEmpty())
		return false;
	return edit->save(fileName);

}

void textEditDialog::closeEvent(QCloseEvent *event){

	if(!edit->isSaved()){
		saveOrNot saveDialog(this, getFile());
	
		saveDialog.exec();
		switch(saveDialog.getState()){
			case saveOrNot::YES:
				if(!saveAs())
					event->ignore();
				else
					event->accept();
				break;
			case saveOrNot::NO:
				event->accept();
				edit->setModified(false);
				break;
			case saveOrNot::CANCEL:
				event->ignore();
				break;
		}
	}

	//if(isModified()){
	//	QMessageBox::StandardButton ret;
	//	ret = QMessageBox::warning(this, 
	//						tr("Soubor byl zmenen"),
	//						tr("Prejete si zapsat zmeny do souboru <b>%1</b>").
	//							arg(edit->getFile()),
	//						QMessageBox::Yes | QMessageBox::No);
	//	if(ret == QMessageBox::Yes)
	//		edit->saveSlot();
	//}

}

aboutDialog::aboutDialog(QWidget *parent): QDialog(parent){

	QVBoxLayout *layout = new QVBoxLayout;
	QLabel *logo = new QLabel;
	QLabel *text = new QLabel;
	QPushButton *button = new QPushButton(tr("Zavřít"));
	QPixmap map(":img/logo.png");
	
	logo->setPixmap(map);
	text->setText(tr(ABOUT_TEXT));
	setLayout(layout);
	layout->addWidget(logo);
	layout->addWidget(text);
	layout->addWidget(button, 0, Qt::AlignCenter);
	connect(button, SIGNAL(clicked()), this, SLOT(buttonClicked()));

}

void aboutDialog::buttonClicked(){

	accept();

}

glDialog::glDialog(QWidget *widget, 
	QString &fileName, 
	gObjectContainer *container, 
	gObjectContainer *buffContainer
	): QDialog(widget){

	QHBoxLayout *layout = new QHBoxLayout;
	
	setWindowTitle(fileName);
	layout->setContentsMargins(0, 0, 0, 0);
	glPreview = new preview(0, 0, fileName, container, buffContainer);
	setLayout(layout);
	layout->addWidget(glPreview, 1);

}

